{{!
  Copyright (c) HashiCorp, Inc.
  SPDX-License-Identifier: BUSL-1.1
~}}

{{#if (or (eq this.policyType "acl") (has-feature "Sentinel"))}}
  <PageHeader as |p|>
    <p.levelLeft>
      <h1 class="title is-3">
        {{uppercase this.policyType}}
        Policies
        {{#if (not-eq this.policyType "acl")}}
          <span class="tag" aria-label="Enforcement level: {{this.model.enforcementLevel}}">
            Sentinel
          </span>
        {{/if}}
      </h1>
    </p.levelLeft>
  </PageHeader>
  <Toolbar>
    {{#if this.model.meta.total}}
      <ToolbarFilters>
        <NavigateInput
          @filterFocusDidChange={{action "setFilterFocus"}}
          @filterDidChange={{action "setFilter"}}
          @filter={{this.filter}}
          @filterMatchesKey={{this.filterMatchesKey}}
          @firstPartialMatch={{this.firstPartialMatch}}
          @extraNavParams={{this.policyType}}
          @placeholder="Filter policies"
          @mode="policy"
        />
        {{#if this.filterFocused}}
          {{#if this.filterMatchesKey}}
            <p class="input-hint">
              <kbd>ENTER</kbd>
              to go to
              <code>{{or this.pageFilter this.filter}}</code>
            </p>
          {{/if}}
          {{#if this.firstPartialMatch}}
            <p class="input-hint">
              <kbd>TAB</kbd>
              to complete
              <code>{{this.firstPartialMatch.id}}</code>
            </p>
          {{/if}}
        {{/if}}
      </ToolbarFilters>
    {{/if}}
    <ToolbarActions>
      <ToolbarLink @route="vault.cluster.policies.create" @type="add" data-test-policy-create-link>
        Create
        {{uppercase this.policyType}}
        policy
      </ToolbarLink>
    </ToolbarActions>
  </Toolbar>
  {{#if this.model.meta.total}}
    {{#each this.model as |item|}}
      {{#if (eq item.id "root")}}
        <div class="list-item-row is-flex" data-test-policy-item>
          <Icon @name="file" class="has-text-grey-light" />
          <div>
            <span class="has-text-weight-semibold has-text-grey" data-test-policy-name>
              {{item.id}}
            </span>
            <p class="help has-text-grey">
              The
              <code>root</code>
              policy does not contain any rules but can do anything within Vault. It should be used with extreme care.
            </p>
          </div>
        </div>
      {{else}}
        <LinkedBlock
          @params={{array "vault.cluster.policy.show" this.policyType item.id}}
          class="list-item-row"
          data-test-policy-link={{item.id}}
        >
          <div class="columns is-mobile">
            <div class="column is-10">
              <LinkTo
                @route="vault.cluster.policy.show"
                @models={{array this.policyType item.id}}
                class="has-text-black has-text-weight-semibold"
                data-test-policy-item={{true}}
              >
                <Icon @name="file" class="has-text-grey-light" />
                <span class="is-underline" data-test-policy-name>{{item.id}}</span>
              </LinkTo>
            </div>
            <div class="column has-text-right">
              <PopupMenu name="policy-nav">
                <Confirm as |c|>
                  <nav class="menu">
                    <ul class="menu-list">
                      {{#if item.updatePath.isPending}}
                        <li class="action">
                          <LoadingDropdownOption />
                        </li>
                        <li class="action">
                          <LoadingDropdownOption />
                        </li>
                      {{else}}
                        {{#if item.canRead}}
                          <li class="action">
                            <LinkTo
                              @route="vault.cluster.policy.show"
                              @models={{array this.policyType item.id}}
                              data-test-policy-link="show"
                            >
                              Details
                            </LinkTo>
                          </li>
                        {{/if}}
                        {{#if item.canEdit}}
                          <li class="action">
                            <LinkTo
                              @route="vault.cluster.policy.edit"
                              @models={{array this.policyType item.id}}
                              data-test-policy-link="edit"
                            >
                              Edit
                            </LinkTo>
                          </li>
                        {{/if}}
                        {{#if item.canDelete}}
                          <li class="action">
                            <c.Message
                              @id={{item.id}}
                              @confirmMessage="This will permanently delete this policy and may affect access to some data"
                              @onConfirm={{action "deletePolicy" item}}
                              data-test-policy-delete={{item.id}}
                            />
                          </li>
                        {{/if}}
                      {{/if}}
                    </ul>
                  </nav>
                </Confirm>
              </PopupMenu>
            </div>
          </div>
        </LinkedBlock>
      {{/if}}
    {{else}}
      <EmptyState @title="No policies matching &quot;{{this.pageFilter}}&quot;" />
    {{/each}}
    {{#unless this.pageFilter}}
      <Hds::Pagination::Numbered
        @currentPage={{this.model.meta.currentPage}}
        @currentPageSize={{this.model.meta.pageSize}}
        @route="vault.cluster.policies.index"
        @showSizeSelector={{false}}
        @totalItems={{this.model.meta.total}}
        @queryFunction={{this.paginationQueryParams}}
      />
    {{/unless}}
  {{else}}
    <EmptyState
      @title="No {{uppercase this.policyType}} policies yet"
      @message="A list of policies will be listed here. Create your first {{uppercase
        this.policyType
      }} policy to get started."
    >
      <LinkTo @route="vault.cluster.policies.create" class="link">
        Create
        {{uppercase this.policyType}}
        policy
        <Chevron />
      </LinkTo>
      <DocLink @path="/vault/tutorials/getting-started/getting-started-policies">
        Learn more
        <Chevron />
      </DocLink>
    </EmptyState>
  {{/if}}
{{else}}
  <UpgradePage @title="Sentinel" @minimumEdition="Vault Enterprise Premium" />
{{/if}}